package com.api.baidudemo;

import java.util.Locale;

import android.text.TextUtils;
import android.util.Log;

public class Logger {

    private static boolean debug = true;

    public static void setDebug(boolean debug) {
        Logger.debug = debug;
    }

    private static String getShortHead(String head) {
        int index = head.lastIndexOf('.');
        String shortHead = null;

        if (index == -1) {
            shortHead = head;
        } else {
            shortHead = head.substring(index + 1);
        }
        shortHead = "[m]" + shortHead;
        return shortHead;
    }

    private static void print(int level, String format, Object... args) {
        if (debug) {
            String lineHead, lineContent;
            // 构造行首
            StackTraceElement trackTrace = new Throwable().getStackTrace()[2];
            lineHead = String.format(Locale.getDefault(), "%s::%s[%d]  ",
                    trackTrace.getClassName(), trackTrace.getMethodName(),
                    trackTrace.getLineNumber());
            // 构造内容
            lineContent = String.format(format, args);

            Log.println(level, getShortHead(lineHead), lineContent);
        }
    }

    public static void log(String format, Object... args) {
//        print(Log.VERBOSE, format, args);
        if (TextUtils.isEmpty(format)) {
            print(Log.VERBOSE, "HTTP null", args);
        } else {
            print(Log.VERBOSE, "HTTP " + format, args);
        }
    }

    public static void error(String str) {
//        print(Log.ERROR, str);
        if (TextUtils.isEmpty(str)) {
            print(Log.VERBOSE, "HTTP null");
        } else {
            print(Log.VERBOSE, "HTTP " + str);
        }
    }

    public static void verbose(String format, Object... args) {
        if (TextUtils.isEmpty(format)) {
            print(Log.VERBOSE, "HTTP null", args);
        } else {
            print(Log.VERBOSE, "HTTP " + format, args);
        }
    }


    // HTTP是过滤的标签
    public static void verbose(String str) {
        if (TextUtils.isEmpty(str)) {
            print(Log.VERBOSE, "HTTP null");
        } else {
            print(Log.VERBOSE, "HTTP " + str);
        }

    }

    public static void error(String format, Object... args) {
//        print(Log.ERROR, format, args);
        if (TextUtils.isEmpty(format)) {
            print(Log.VERBOSE, "HTTP null", args);
        } else {
            print(Log.VERBOSE, "HTTP " + format, args);
        }
    }

    public static void logHexData(String title, byte[] b) {
        logHexData(title, b, 0, b.length);
    }

    public static void logHexData(String title, byte[] b, int offset, int length) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(title);
        stringBuffer.append(" len:" + length + ",data:[");
        for (int i = offset; i < offset + length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            stringBuffer.append(hex.toUpperCase(Locale.getDefault()) + " ");
        }
        stringBuffer.append("]");
    }
}
